﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace IssueServiceLibrary
{
    /// <summary>
    /// These methods are used as a part of the WCF functionality of the application.
    /// </summary>
    [ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
    public class IssueService : IIssueService
    {
        IssueList issueList = IssueList.ReadIssuesSerializable();
        UserList userList = UserList.ReadUsersSerializable();

        #region IIssueService Members

        //Returns the list of Issues
        public List<Issue> GetAllIssues()
        {
            return issueList.Issues;
        }

        //Gets all issues assigned to a specific user
        public List<Issue> GetIssuesFor(User aUser)
        {
            List<Issue> currentIssues = new List<Issue>();

            foreach (Issue issue in issueList.Issues)
            {
                if (issue.UserAssignedTo != null && aUser.Equals(issue.UserAssignedTo))
                {
                    currentIssues.Add(issue);
                }
            }
            return currentIssues;
        }

        //Adds a new issue to the list of issues
        public bool AddIssue(Issue aIssue)
        {
            aIssue.IssueId = System.Guid.NewGuid().ToString();
            issueList.Issues.Add(aIssue);
            IssueList.WriterIssuesSerializable(issueList);
            return true;
        }

        //Removes an issue from the list of issues
        public bool RemoveIssue(Issue aIssue)
        {
            issueList.Issues.Remove(aIssue);
            IssueList.WriterIssuesSerializable(issueList);
            return true;
        }

        //Assigns a specific user to a specific issue
        public bool AssignIssue(Issue aIssue, User aUser)
        {
            if ((aIssue != null) && (aUser != null))
            {
                aIssue.UserAssignedTo = aUser;
                return true;
            }
            else
            {
                return false;
            }
        }
       
        //Logs a user in given a userID and password
        public User Login(string userID, string password)
        {
            User tempUser = new User();
            tempUser.LoginId = userID;
            tempUser.password = password;

            int index = userList.Users.IndexOf(tempUser);
            if (index != -1)
                return userList.Users.ElementAt(index);
            else
               return null;
        }

        //Adds a user to the system
        public bool AddUser(User aUser)
        {
            userList.Users.Add(aUser);
            UserList.WriterUsersSerializable(userList);
            return true;
        }

        //Removes a user from the system
        public bool RemoveUser(User aUser)
        {
            userList.Users.Remove(aUser);
            UserList.WriterUsersSerializable(userList);
            return true;
        }

        //Returns a specific user of a certain username
        public User GetUser(string username)
        {
           foreach (User user in userList.Users)
            {
                if (username.Equals(user.LoginId,StringComparison.OrdinalIgnoreCase))
                {
                    return user;
                }
            }
            return null;
        }

        //Returns list of users
        public List<User> GetAllUsers()
        {
            return this.userList.Users;
        }

        //Checks to see if a userID is already in use
        public bool LoginIdExists(string user)
        {
            bool exist = false;
            foreach (User temp in userList.Users)
            {
                if (user != null && user.Equals(temp.LoginId, StringComparison.OrdinalIgnoreCase))
                {
                    exist = true;
                    return exist;
                }
            }
            return exist;
        }

        //Generates a new ID
        public int GetNextID()
        {
            int toReturn = Issue.nextID;
            Issue.nextID += 1;
            return toReturn;
        }

        //Edits a specific issue in the WCF
        public bool EditIssue(Issue anIssue)
        {
            int realIssueIndex = this.issueList.Issues.IndexOf(anIssue);
            if (realIssueIndex != -1)
            {
                Issue realIssue = this.issueList.Issues.ElementAt(realIssueIndex);
                realIssue.CreationDate = anIssue.CreationDate;
                realIssue.Description = anIssue.Description;
                realIssue.DueDate = anIssue.DueDate;
                realIssue.HoursLeftToGo = anIssue.HoursLeftToGo;
                realIssue.HoursWorked = anIssue.HoursWorked;
                realIssue.Name = anIssue.Name;
                realIssue.UserAssignedTo = anIssue.UserAssignedTo;
                return true;
            }
            else
                return false;
        }
        
        #endregion

    }
}
